package org.example.yiyu.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.yiyu.exception.CustomException;
import org.example.yiyu.model.Diary;
import org.example.yiyu.model.DiaryQuery;
import org.example.yiyu.model.DiaryRe;
import org.example.yiyu.response.BaseResponse;
import org.example.yiyu.service.DiaryService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequiredArgsConstructor
@RequestMapping("/diary")
public class DiaryController {

    private final DiaryService diaryService;

    // 创建日记,并与标签关联
    @PostMapping
    public BaseResponse<Long> createDiary(@RequestBody Diary diary, HttpServletRequest request) {

        //从拦截器中获得令牌解析的userId
        Long userId = (Long) request.getAttribute("userId");
        diary.setUserId(userId);

        log.info("id为{}的用户创建日记",userId);

        Long id;
        try {
            id=diaryService.addDiary(diary);
        } catch (Exception e) {
            throw new CustomException(500,"创建日记失败");
        }

        return new BaseResponse<Long>("创建日记成功",true,id);
    }

    // 删除日记（假删除）
    @DeleteMapping("/{id}")
    public BaseResponse<String> deleteDiary(@PathVariable Long id) {
        log.info("删除日记{}",id);

        diaryService.removeDiary(id);

        return new BaseResponse<String>("删除日记成功",true,null);
    }

    // 更新日记
    @PutMapping
    public BaseResponse<String> updateDiary(@RequestBody Diary diary,HttpServletRequest request) {

        //从拦截器中获得令牌解析的userId
        Long userId = (Long) request.getAttribute("userId");
        diary.setUserId(userId);


        log.info("id为{}的用户更新了日记{}" +
                ",内容为{}",userId,diary.getId(),diary.getContent());

        diaryService.modifyDiary(diary);

        return new BaseResponse<String>("更新日记成功",true,null);
    }

    // 根据ID获取日记详情
    @GetMapping("/{id}")
    public BaseResponse<DiaryRe> getDiary(@PathVariable Long id) {

        log.info("查询id为{}的日记",id);

        Diary diary=new Diary();
        diary=diaryService.getDiary(id);

        DiaryRe diaryRe=new DiaryRe(diary.getId(), diary.getContent(),diary.getCreateTime(),diary.getUpdateTime());
        return new BaseResponse<DiaryRe>("查找日记成功",true,diaryRe);
    }

    // 动态查询日记列表
    @GetMapping
    public BaseResponse<List<Diary>> findDiaries(
            // @RequestParam Long userId,
            @RequestParam(required = false) Integer tagId,//标签查询
            @RequestParam(required = false) String content,//文本模糊查询
            @RequestParam(required = false) LocalDateTime startTime,//起始时间
            @RequestParam(required = false) LocalDateTime endTime,//结束时间
            @RequestParam(required = false, defaultValue = "createTime") String sortField,//排序字段
            @RequestParam(required = false, defaultValue = "DESC") String sortOrder,//排序方式
            @RequestParam(required = false, defaultValue = "1") Integer page,//页数
            @RequestParam(required = false, defaultValue = "10") Integer limit,//分页每页数
            HttpServletRequest request                  ) {

        //从拦截器中获得令牌解析的userId
        Long userId = (Long) request.getAttribute("userId");

        DiaryQuery query = new DiaryQuery();
        query.setUserId(userId);
        query.setTagId(tagId);
        query.setContent(content);
        query.setStartTime(startTime);
        query.setEndTime(endTime);
        query.setSortField(sortField);
        query.setSortOrder(sortOrder);
        query.setPage(page);
        query.setLimit(limit);

        log.info("id为{}的用户正在查询日记列表",userId);

        return new BaseResponse<List<Diary>>("范围查找成功",true,diaryService.searchDiaries(query));

    }


}










